#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>

int Compare(int year, int month, int day);

int main(int argc, char* argv[])
{
    int date, year, month, day, p, a, flag1 = 0, flag2 = 0;
    scanf("%d", &date);
    year = date / 10000;
    month = (date % 10000) / 100;
    day = date % 100 + 1;
    for (; year < 9999; year++)
    {

        for (; month <= 12; month++)
        {
            if (month == 4 || month == 6 || month == 9 || month == 11)
                p = 30;
            else if ((month == 2) && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
                p = 29;
            else if (month == 2)
                p = 28;
            else
                p = 31;
            for (; day <= p; day++) {
                a = Compare(year, month, day);
                if ((a == 1) && (flag1 == 0))
                {
                    printf("%d\n", year * 10000 + month * 100 + day);
                    flag1 = 1;
                }
                if ((a == 2) && (flag2 == 0))
                {
                    printf("%d\n", year * 10000 + month * 100 + day);
                    flag2 = 1;
                }
                if ((flag1 == 1) && (flag2 == 1))
                    goto END;
            }
            day = 1;
        }
        month = 1;
    }
END:;
    return 0;
}


int Compare(int year, int month, int day)
{
    int a[4], b[2], c[2];
    a[0] = year / 1000;
    a[1] = (year % 1000) / 100;
    a[2] = (year % 100) / 10;
    a[3] = year % 10;
    b[0] = month / 10;
    b[1] = month % 10;
    c[0] = day / 10;
    c[1] = day % 10;
    if ((a[1] == a[3]) && (a[3] == b[0]) && (b[0] == c[0]) && (a[0] == a[2]) && (a[2] == b[1]) && (b[1] == c[1]))
        return 2;
    if ((a[3] == b[0]) && (a[2] == b[1]) && (a[1] == c[0]) && (a[0] == c[1]))
        return 1;
    else
        return 0;
}